<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <div v-if="isDeleteShow">
        <span @click="clearSearchHistroies"> 全部删除 </span>
        &nbsp;&nbsp;
        <span @click="isDeleteShow = false">完成</span>
      </div>
      <van-icon name="delete" v-else @click="isDeleteShow = true"></van-icon>
    </van-cell>
    <van-cell
      :title="item"
      v-for="(item, index) in searchHistories"
      :key="index"
      @click="onSearchItemClick(item, index)"
    >
      <van-icon name="close" v-show="isDeleteShow" />
    </van-cell>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDeleteShow: false
    }
  },
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },

  methods: {
    // 搜索条目的点击回调
    onSearchItemClick(item, index) {
      if (this.isDeleteShow) {
        // 删除状态点击删除
        this.searchHistories.splice(index, 1)
      } else {
        // 非删除状态点击跳转
        // console.log('跳转')
        this.$emit('search', item)
      }
    },
    // 清除历史记录点击回调
    clearSearchHistroies() {
      this.$dialog.confirm({ title: '是否删除历史记录' }).then(
        () => this.searchHistories.splice(0),
        (err) => console.log(err)
      )
    }
  }
}
</script>

<style lang="less" scoped></style>
